package org.freelancer.app.dao;

import java.util.List;

import org.freelancer.app.core.hbn.GenericDao;
import org.freelancer.app.models.Venta;
import org.freelancer.app.models.VentaTemporal;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class VentaTemporalDao extends GenericDao<VentaTemporal, Long>{
	
	public VentaTemporalDao() {
		super(VentaTemporal.class);
	}
	
	public List<VentaTemporal> ventaTemporalDao(){
		return getAll();
	}
	
	public List<VentaTemporal> comprobarcbvtatemporal(Long cb){
		@SuppressWarnings("unchecked")
		List<VentaTemporal> vtastempbycb=getSessionFactory().getCurrentSession().createCriteria(VentaTemporal.class)
        		.add(Restrictions.eq("cb", cb))
        		.setProjection(Projections.property("cb"))
        		.list();
	    	return vtastempbycb;
	    	  
     	
	}
	public void acutalizarVtasTemporales(Long cb){
		Criteria criteria=getSessionFactory().getCurrentSession().createCriteria(VentaTemporal.class);
		Criterion criterio=Restrictions.eq("cb", cb);
		criteria.add(criterio);
		VentaTemporal vtatemp=(VentaTemporal)criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).uniqueResult();
		vtatemp.setCantidad(sumacantidad(vtatemp.getCantidad()));
		vtatemp.setTotal(vtatemp.getPrecioventa()*vtatemp.getCantidad());
	    getHibernateTemplate().saveOrUpdate(vtatemp);
		
	}
	public List<VentaTemporal> sumaTotal(){
		List<VentaTemporal> sumatotal=this.getSessionFactory().getCurrentSession().createCriteria(VentaTemporal.class)
				.setProjection(Projections.sum("total"))
				.list();
		 return sumatotal;
	}
	public int sumacantidad(int p){
		int pa=0;
		return pa=p+1;
	}
	
	public void guardarVentaTemporal(VentaTemporal vtatemp){
		
		save(vtatemp);
	}
	public void finalizarVentaDao(Venta venta){
		VentaDao ventad=new VentaDao();
		ventad.finalizarVentadao(venta);
	}

	
	
}
